﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Extend.SqlString;

namespace QL_Truong_Hoc
{
    internal static class Program
    {
        public static Database gobjDb = null;
        public static DbConnection gobjConn = null;
        public static DbTransaction gobjTrans = null;
        public static bool gblnLoggedIn = false;
        public static string gstrCurrentUser = "";
        public static clsEvents gevnApplication;
        public static List<string> glstRoles;
        public static string PubConnect = string.Empty;
        public static string NameDatabase = string.Empty;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main()
        {
            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                DevExpress.Skins.SkinManager.EnableFormSkins();
                DevExpress.UserSkins.BonusSkins.Register();
                //UserLookAndFeel.Default.SetSkinStyle("DevExpress Style");
                UserLookAndFeel.Default.SetSkinStyle("Office 2010 Silver");
                //UserLookAndFeel.Default.SetSkinStyle("Office 2010 Blue");
                //UserLookAndFeel.Default.SetSkinStyle("Metropolis");
                //UserLookAndFeel.Default.SetSkinStyle("Whiteprint");

                gobjDb = EnterpriseLibraryContainer.Current.GetInstance<Database>("QL_Truong_Hoc.Properties.Settings.QLTHConnectionString");
                PubConnect = gobjDb.ConnectionString;
                DtConnection Con = new DtConnection();
                Con.ConnectString = PubConnect;
                Command.Connect = Con;
                gobjConn = gobjDb.CreateConnection();
                gevnApplication = new clsEvents();
                glstRoles = new List<string>();
                try
                {
                    gobjConn.Open();
                }
                catch
                {
                    clsCommon.fncMsgError("Không thể kết nối được đến cơ sở dữ liệu. Xin hãy kiểm tra lại.");
                    using (frmConnectionSetting objSetting = new frmConnectionSetting())
                    {
                        objSetting.ShowDialog();
                        if (!objSetting.SettingSaved) return;
                    }
                }

                NameDatabase = gobjConn.Database.ToString();
                using (frmLogin objLogin = new frmLogin())
                {
                    objLogin.ShowDialog();
                    if (!objLogin.LoginSuccess)
                    {
                        gobjConn.Close();
                        return;
                    }
                }

                Application.Run(new frmMain());

                gobjConn.Close();
            }
            catch { clsCommon.fncMsgError("Xảy ra lỗi không mong muốn. Chương trình sẽ bị đóng."); }
        }
    }
}